<?php

namespace app\common\service\Users;

use app\common\entity\Mywallet;
use app\common\entity\User;
use app\common\entity\UserInviteCode;
use think\facade\Request;
use think\facade\Session;
use app\common\entity\MywalletLog;

class Service
{
    /**
     * 加密前缀
     */
    const PREFIX_KEY = "eco_member";

    /**
     * 加密函数
     */
    public function getPassword($password)
    {
        return md5(md5(self::PREFIX_KEY . $password));
    }

    /**
     * 验证密码
     */
    public function checkPassword($password, User $entity)
    {
        return $this->getPassword($password) === $entity->getPassword();
    }

    public function checkSafePassword($password, User $entity)
    {
        return $this->getPassword($password) === $entity->getSafePassword();
    }

    public function addUser($data)
    {
        $walletbao = '0L' . $this->str_rand();

        $entity = new User();
        $entity->mobile = $data['mobile'];
        if($data['robot'] == 1){
            $inviteCode = new UserInviteCode();
            $entity->nick_name = $inviteCode->makeCode(1);
        }else{
            $entity->nick_name = $data['mobile'];
        }

        $entity->avatar = $data['photo']??'/images/header.png';
        $entity->password = $this->getPassword($data['password']);
        $entity->trad_password = $this->getPassword($data['trad_password']);
       /* $entity->wx = $data['wx'] ?? '';
        $entity->zfb = $data['zfb'] ?? '';
        $entity->wx_image_url = $data['wx_image_url'] ?? '';
        $entity->zfb_image_url = $data['zfb_image_url'] ?? '';
        $entity->card_name = $data['card_name'] ?? '';
        $entity->card = $data['card'] ?? '';
        $entity->real_name = $data['real_name'] ?? '';
        $entity->card_id = $data['card_id'] ?? '';
        $entity->level = $data['level'];*/
        $entity->status = User::STATUS_DEFAULT;
        $entity->robot = $data['robot'];
       /* $entity->real_pass = $data['real_pass'];
        $entity->is_active = $data['is_active'];
        $entity->active_time = time();*/
        $entity->register_time = time();
        $entity->register_ip = $data['ip'] ?? Request::ip();
        $entity->pid = User::where('mobile', $data['p_nick_name'])->value('id')?? 0;


        if ($entity->save()) {
            // User::upLevel($entity->pid);

            $my_wallet_id = Mywallet::insert(['user_id' => $entity->id]);
            if($my_wallet_id){
                \app\common\shop\UserLevelModel::reg($entity->id,$entity->pid);
            }

            return $entity->getId();
        }

        return false;
    }


    function str_rand($length = 30, $char = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
    {
        if (!is_int($length) || $length < 0) {
            return false;
        }

        $string = '';
        for ($i = $length; $i > 0; $i--) {
            $string .= $char[mt_rand(0, strlen($char) - 1)];
        }

        return $string;
    }


    public function updateUser(User $user, $data)
    {
        $user->nick_name = $data['nick_name'];
        if ($data['password']) {
            $user->password = $this->getPassword($data['password']);
        }

        if ($data['trad_password']) {
            $user->trad_password = $this->getPassword($data['trad_password']);
        }

        $user->avatar = $data['photo'];
        $user->wx = $data['wx'] ?? '';
        $user->zfb = $data['zfb'] ?? '';
        $user->wx_image_url = $data['wx_image_url'] ?? '';
        $user->zfb_image_url = $data['zfb_image_url'] ?? '';
        $user->card_name = $data['card_name'] ?? '';
        $user->card_number = $data['card_number'] ?? '';
        $user->real_name = $data['real_name'] ?? '';
        $user->card_id = $data['card_id'] ?? '';
        $user->level = $data['level'] ?? '';
        $user->active_time = time();
        $user->robot = $data['robot'];
        $user->team_level = $data['team_level']?? 0;
        $user->agent_level = $data['agent_level']?? 0;
        $user->house_level = $data['house_level']?? 0;
        $user->area_level = $data['area_level']?? 0;
        $user->partner_level = $data['partner_level']?? 0;
        $user->province = $data['province']?? 0;
        $user->city = $data['city']?? 0;
        $user->area = $data['area']?? 0;

        if($data['agent_over_time']){
            $user->agent_over_time = strtotime($data['agent_over_time']);
        }
        if($data['house_over_time']){
            $user->house_over_time = strtotime($data['house_over_time']);
        }
        if($data['area_over_time']){
            $user->area_over_time = strtotime($data['area_over_time']);
        }
        if($data['partner_over_time']){
            $user->partner_over_time = strtotime($data['partner_over_time']);
        }
        return $user->save();
    }

    /**
     * 检查用户名是否已存在
     */
    public function checkMobile($name, $id = 0)
    {
        $entity = user::where('mobile', $name);
        if ($id) {
            $entity->where('id', '<>', $id);
        }
        return $entity->find() ? true : false;
    }

    public function checkNickName($name, $id = 0)
    {
        $entity = user::where('nick_name', $name);
        if ($id) {
            $entity->where('id', '<>', $id);
        }
        return $entity->find() ? true : false;
    }

    /**
     * 银行卡号 微信号 支付宝账号 唯一
     */
    public function checkMsg($type, $account, $id = '')
    {
        return \app\common\entity\User::where("$type", $account)->where('id', '<>', $id)->find();
    }
    // 支付密码
    public function checkPayPassword($password, User $entity)
    {
        return $this->getPassword($password) === $entity->trad_password;
    }
}